package com.kehao.leetcode.algorithm;

import org.junit.Test;


//https://leetcode-cn.com/problems/median-of-two-sorted-arrays/
public class Solution4 {

    @Test
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int length = nums1.length + nums2.length;
        int[] num_total = new int[length];
        int index_num1= 0,index_num2 = 0;
        int index_total = 0;
        for(;index_num1<nums1.length&&index_num2< nums2.length;){
            if(nums1[index_num1]<nums2[index_num2]){
                num_total[index_total] = nums1[index_num1];
                index_num1++;
                index_total++;
            }else if(nums1[index_num1]>nums2[index_num2]){
                num_total[index_total] = nums2[index_num2];
                index_num2++;
                index_total++;
            }else {
                num_total[index_total] = nums1[index_num1];
                index_num1++;
                index_total++;
                num_total[index_total] = nums2[index_num2];
                index_num2++;
                index_total++;

            }
        }
        while (index_num1<nums1.length){
            num_total[index_total] = nums1[index_num1];
            index_num1++;
            index_total++;
        }
        while (index_num2< nums2.length){
            num_total[index_total] = nums2[index_num2];
            index_num2++;
            index_total++;
        }
        if(length%2 == 1){
            return num_total[length/2];
        }else {
            return (num_total[length/2-1]+num_total[length/2])/2.0;
        }
    }
}
